//
//  XJButton.m
//  AppFrame
//
//  Created by GXJ on 2017/2/24.
//  Copyright © 2017年 GXJ. All rights reserved.
//

#define kButtonImageRatio 0.6

#import "XJButton.h"
#import "UIImage+XJImageToolBox.h"
#import "XJBadgeButton.h"

@interface XJButton()

@property (nonatomic, weak)XJBadgeButton *badgeButton;

@end

@implementation XJButton

- (instancetype)initWithFrame:(CGRect)frame andIsTabItem:(BOOL)isTab
{
    self = [super initWithFrame:frame];
    if (self) {
        
        if (isTab) {
            self.imageView.contentMode = UIViewContentModeCenter;
            self.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:20*m6Scale];
        } else {
            self.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:20*m6Scale];
        }
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor colorWithHexString:@"1e91fc"] forState:UIControlStateSelected];
        
        XJBadgeButton *badgeButton = [XJBadgeButton buttonWithType:UIButtonTypeCustom];
        
        badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self addSubview:badgeButton];
        self.badgeButton = badgeButton;
    }
    return self;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    //    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height * kButtonImageRatio;
    return CGRectMake(contentRect.size.width / 2 - imageH / 2, 2, imageH, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = contentRect.size.height * kButtonImageRatio;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height - titleY;
    return CGRectMake(0, titleY-2, titleW, titleH);
}

- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    // KVO
    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
    [item addObserver:self forKeyPath:@"title" options:0 context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    [self setTitle:self.item.title forState:UIControlStateNormal];
    [self setTitle:self.item.title forState:UIControlStateSelected];
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    
    self.badgeButton.badgeValue = self.item.badgeValue;
    
    CGFloat badgeY = 1;
    CGFloat badgeX = self.frame.size.width - self.badgeButton.frame.size.width - 8;
    CGRect badgeFrame = self.badgeButton.frame;
    badgeFrame.origin.x = badgeX;
    badgeFrame.origin.y = badgeY;
    self.badgeButton.frame = badgeFrame;
}

- (void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"badgeValue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}

@end
